﻿using ChuanYe.CountyFireManagement.EF.MySQL;
using ChuanYe.CountyFireManagement.EF.MySQL.Models;
using ChuanYe.CountyFireManagement.Model;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ChuanYe.CountyFireManagement.BLL
{
    public class GpsDeviceRecordBLL
    {

        #region 构造单例
        private GpsDeviceRecordBLL() { }
        private static GpsDeviceRecordBLL _instance;
        public static GpsDeviceRecordBLL Instance
        {
            get
            {
                return _instance ?? (_instance = new GpsDeviceRecordBLL());
            }
        }
        #endregion


        public int AddList(List<Gpsdevicerecord> infos) 
        {
            using (CountyFireManagementContext ef = new CountyFireManagementContext())
            {
                foreach (var item in infos)
                {
                    ef.Gpsdevicerecord.Add(item);
                    ef.Entry<Gpsdevicerecord>(item).State = EntityState.Added;
                }
                return ef.SaveChanges();
            }
        }

        public List<GpsDeviceRecordModel> GetByPage(string gpsDeviceNo, RequestPageBase page)
        {
            using (CountyFireManagementContext ef = new CountyFireManagementContext())
            {
                var sql = " select a.*,b.GpsDeviceNo from gpsdevicerecord as a ";
                sql += " left join gpsdevice as b on a.GpsDeviceID =b.GpsDeviceID  ";
                sql += " where 1=1  {0} ";
                string paramsSql = string.Empty;
                if (!string.IsNullOrEmpty(gpsDeviceNo))
                {
                    paramsSql += $" and b.GpsDeviceNo='{gpsDeviceNo}'";
                }
                paramsSql += page.TimeSql(" a.CreateTime ");
                sql = string.Format(sql, paramsSql);
                var countSql = sql.CountSql();
                page.TotalRows = Convert.ToInt32(ef.Database.SqlQuery(countSql).Rows[0][0]);
                sql += page.OrderByPattern == 0 ? "a.CreateTime".OrderByDescending() : "a.CreateTime".OrderBy();
                sql += page.Limit();
                var list = ef.Database.SqlQuery<GpsDeviceRecordModel>(sql);
                return list;
            }
        }

        public List<GpsDeviceModel> GetGpsDevice()
        {
            using (CountyFireManagementContext ef = new CountyFireManagementContext())
            {
                var sql = " select a.*,b.BindType,b.BindId, ";
                sql += " (case b.BindType when 0 then d.RealName when 1 then c.CarName end)as BindName,  ";
                sql += " (if (b.BindType is null,0,1))as IsBind  ";
                sql += " from gpsdevice as a ";
                sql += " left join gpsdeviceconfig as b on a.GpsDeviceID =b.GpsDeviceID ";
                sql += " left join carinfo as c on b.BindID=c.CarName ";
                sql += " left join userinfo as d on b.BindID=d.UserID ";
                sql +="a.CreateTime".OrderByDescending();
                var list = ef.Database.SqlQuery<GpsDeviceModel>(sql);
                return list;
            }
        }
    }
}
